 /**
 * DISCLIMER: 
 * this library is an opensource, feel free to edit or redistribute it
 * but use it with you own risk
 * 
 * ktutnik
 */
package org.ticlib.task
{
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ServiceInvoker extends InvokerBase 
	{
		[Bindable] public var operation:AbstractOperation;
		
		private var responder:Responder;
		
		public function ServiceInvoker()
		{
			super();
			responder = new Responder(onResult, onFault);
		}
		
		override public function stop():void
		{
			if(!isCanceled)
			{
				operation.cancel();
				isCanceled = true;
			}
			signalCancel();
		}
		
		override public function execute():void
		{
			initialize();
			
			if(isUseCache()) return;

			operation.arguments = this.arguments;
			var token:AsyncToken = operation.send();
			token.addResponder(responder);
		}
		
		private function onResult(e:ResultEvent):void
		{
			signalComplete(e.result);
		}
		
		private function onFault(e:FaultEvent):void
		{
			faultObject = e.fault;
			dispatchEvent(FaultEvent.createEvent(e.fault));
		}
	}
}